﻿namespace HZY.Host.Admin.Controllers;

/// <summary>
/// 测试缓存AOP
/// </summary>
public class TestCacheController : ControllerBase
{

    private static Dictionary<string, object> values = [];

    [HttpGet("{key}")]
    [Cacheable(CacheKey = "Test:{key}")]
    public string GetTest([FromRoute] string key)
    {
        if (values.TryGetValue(key, out var value))
        {
            return value.ToString();
        }

        return "null";
    }

    [HttpGet("{key}/{value}")]
    public void SetTest([FromRoute] string key, [FromRoute] string value)
    {
        values.TryAdd(key, value);
    }

    [HttpGet("{key}")]
    [CacheEvict(CacheKey = "Test:{key}")]
    public void DeleteTest([FromRoute] string key)
    {
        values.Remove(key);
    }

    [HttpPost()]
    [CacheEvict(CacheKey = "Test:{aa.Keys}")]
    public void DeleteBatchTest([FromBody] Simple simple)
    {
        foreach (var key in simple.Keys)
        {
            values.Remove(key);
        }
    }
}

public class Simple
{
    public List<string> Keys { get; set; } = [];
}